using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentChunks : UserControl, DemoControl 
    {
        public RichTextBox oTextBox;

        public ctrlGetDocumentChunks(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }

        public void processText()
        {
            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                SalienceDocumentDetails oDetails = SalienceDemo.oSalienceSession.GetDocumentDetails();

                //Now draw up the html
                string sHtml = "<html><head><style>";
                sHtml += ".sentence {margin-bottom: 200}\n";
                sHtml += ".np {background-color: cyan; cursor:default}\n";
                sHtml += ".passive {background-color: blue}\n";
                sHtml += ".inf {background-color: yellow}\n";
                sHtml += ".copula {background-color: lightgreen}\n";
                sHtml += ".vp {background-color: magenta}\n";
                sHtml += ".pp {background-color: yellow}\n";
                sHtml += ".neg {background-color: red}\n";
                sHtml += ".cnp {background-color: orange}\n";
                sHtml += ".jp {background-color: pink}\n";
                sHtml += ".rp {background-color: lightpink}\n";
                sHtml += ".punct {background-color: lightgray}\n";
                sHtml += ".none {background-color: white}\n";
                sHtml += ".head {font-weight: bold;background-color: transparent}\n";
                sHtml += ".neg {background-color: red}\n";
                sHtml += "body { font-family: Trebuchet MS; font-size: "+Properties.Settings.Default.fFontSize.ToString()+"pt; color:#000000; background-color:#FFFFFF}\n";
                sHtml += "</style></head><body>";

                int nSentence = 1;
                //TODO: Rewrite this markup
                foreach (SalienceChunk oChunk in oDetails.Chunks)
                {
                    if (oChunk.nSentence != nSentence)
                    {
                        sHtml += "<br/>";
                        nSentence = oChunk.nSentence;
                    }

                    sHtml += "<span class=\"" + oChunk.sLabel + "\" title=\"" + getSpanLabel(oChunk.sLabel) + "\">[";
                    string sTemp = "";
                    foreach (SalienceWord oToken in oChunk.Tokens)
                    {
                        if (oToken.bInvert)
                        {
                            sTemp += "<span class=\"neg\">" + oToken.sToken + "_" + oToken.sPOSTag + "</span> ";
                        }
                        else
                        {
                            sTemp += oToken.sToken + "_" + oToken.sPOSTag + " ";
                        }
                    }
                    sHtml += sTemp.Substring(0, sTemp.Length - 1);
                    sHtml += "]</span>&nbsp;";
                }
               
                sHtml += "</body></html>";

                results.DocumentText = sHtml;
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        private string getSpanLabel(string sLabel)
        {
            string sRet = "";
            if (sLabel=="np" || sLabel== "cnp")
            {
               sRet = "Noun Phrase (np)";
            }
            else if (sLabel == "cnp")
            {
                sRet = "Noun Phrase (cnp)";
            }
            else if (sLabel=="passive")
            {
               sRet = "Passive";
            }
            else if (sLabel=="inf")
            {
               sRet = "Inf";
            }
            else if (sLabel=="copula")
            {
               sRet = "Copula";
            }
            else if (sLabel=="vp")
            {
               sRet = "Verb Phrase (vp)";
            }
            else if (sLabel=="pp")
            {
               sRet = "pp";
            }
            else if (sLabel=="neg")
            {
               sRet = "Negative (neg)";
            }
            else if (sLabel=="jp")
            {
               sRet = "Adjective Phrase (jp)";
            }
            else if (sLabel=="rp")
            {
               sRet = "rp";
            }
            else if (sLabel=="punct")
            {
               sRet = "Punctuation (punct)";
            }
            return sRet;
        }

        private void getDocumentSentiment_VisibleChanged(object sender, EventArgs e)
        {
            results.DocumentText = "";
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.NOTHING);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            if (this.results.Document.Body != null)
            {
                Graphics g = this.CreateGraphics();
                float pixels = Properties.Settings.Default.fFontSize * g.DpiX / 72;
                g.Dispose();
                this.results.Document.Body.Style= "font-size:" + pixels + "px;";
            }
        }

        public void EnableProcessButton()
        {
            // Process button is enabled if text box has text
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }
        #endregion
    }
}
